﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace TimeApp
{
    public partial class MainForm : Form
    {

        private TimeForm timeForm = new TimeForm();
        private bool DisplayState = false;
        private IniFiles ini = new IniFiles(Application.StartupPath + @"\MyConfig.INI");

        public MainForm()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            //窗体样式
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "数字时钟";

            //事件
            this.SetupBut.Click += SetupBut_Click;
            this.ViewBut.Click += ViewBut_Click;

            //绑定数据
            this.BindData();

            //初始化
            this.timeForm.SetViewFont(this.comboFormat.SelectedValue.ToString(), "LCDMU.TTF",
                this.trackBar1.Value,
                (Color)comboTextColor.SelectedValue, (Color)comboBakColor.SelectedValue);

        }

   

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindData()
        {
            //绑定时间显示格式
            comboFormat.DataSource = ClockStyle.ViewFormat;
            comboFormat.DisplayMember = "DisplayText";
            comboFormat.ValueMember = "RealValue";
            String ViewFormat =  ini.IniReadValue("ClockSetup", "ViewFormat");
            comboFormat.SelectedIndex = ViewFormat==""?0 : Convert.ToInt32(ViewFormat);//显示样式

            //背景颜色绑定
            comboBakColor.DataSource = ClockStyle.ViewColor;
            comboBakColor.DisplayMember = "DisplayText";
            comboBakColor.ValueMember = "RealValue";
            String BakColor = ini.IniReadValue("ClockSetup", "BakColor");
            comboBakColor.SelectedIndex = BakColor==""?0:Convert.ToInt32(BakColor);

            //文本颜色绑定
            comboTextColor.DataSource = ClockStyle.ViewColor;
            comboTextColor.DisplayMember = "DisplayText";
            comboTextColor.ValueMember = "RealValue";
            String TextColor = ini.IniReadValue("ClockSetup", "TextColor");
            comboTextColor.SelectedIndex = TextColor == "" ? 0 : Convert.ToInt32(TextColor);

            //字体选择
            DirectoryInfo theFolder = new DirectoryInfo(System.Environment.CurrentDirectory);  // 给出你的目录文件位置  
            FileInfo[] fileInfo = theFolder.GetFiles(); // 获得当前的文件夹内的所有文件数组
            List<TextAndValue> fontList = new List<TextAndValue>();
            foreach (FileInfo NextFile in fileInfo)   //遍历文件
            {
                if (NextFile.Extension.ToUpper() == ".ttf".ToUpper())
                {
                    fontList.Add(new TextAndValue(NextFile.Name, NextFile.Name));
                }
            }
            ClockStyle.ViewFont = fontList;
            if (fontList.Count > 0)
            {
                comboFont.DataSource = ClockStyle.ViewFont;
                comboFont.DisplayMember = "DisplayText";
                comboFont.ValueMember = "RealValue";
                String FontName = ini.IniReadValue("ClockSetup", "Font");
                comboFont.SelectedIndex = FontName==""?0:Convert.ToInt32(FontName);//选择字体
            }


            //改变字体大小控件
            this.trackBar1.Minimum = 20;
            this.trackBar1.Maximum = 400;
            this.trackBar1.TickStyle = TickStyle.None;//不显示下面的刻度
            String FontSize = ini.IniReadValue("ClockSetup", "FontSize");
            this.trackBar1.Value = FontSize == "" ? 0 :Convert.ToInt32(FontSize);            
            this.trackBar1.ValueChanged += TrackBar1_ValueChanged;
            this.FontSizeLabel.Text = "字体:" + this.trackBar1.Value;
        }

        //显示滑动值
        private void TrackBar1_ValueChanged(object sender, EventArgs e)
        {
            this.FontSizeLabel.Text = "字体:" + this.trackBar1.Value;
        }

        //设置按钮
        private void SetupBut_Click(object sender, EventArgs e)
        {
            this.timeForm.SetViewFont(this.comboFormat.SelectedValue.ToString(),
                this.comboFont.SelectedValue.ToString(), this.trackBar1.Value,
                (Color)comboTextColor.SelectedValue, (Color)comboBakColor.SelectedValue);
            //写入配置文件
            ini.IniWriteValue("ClockSetup", "ViewFormat", this.comboFormat.SelectedIndex.ToString());
            ini.IniWriteValue("ClockSetup", "Font", this.comboFont.SelectedIndex.ToString());
            ini.IniWriteValue("ClockSetup", "FontSize", this.trackBar1.Value.ToString());
            ini.IniWriteValue("ClockSetup", "TextColor", this.comboTextColor.SelectedIndex.ToString());
            ini.IniWriteValue("ClockSetup", "BakColor", this.comboBakColor.SelectedIndex.ToString());
        }

        //显示按钮
        private void ViewBut_Click(object sender, EventArgs e)
        {
            if (DisplayState)
            {
                DisplayState = false;
                this.ViewBut.Text = "显示";
                this.timeForm.Hide();
            }
            else
            {
                DisplayState = true;
                this.ViewBut.Text = "关闭";
                this.timeForm.Show();
            }
        }
    }
}
